/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MobileCLient;

import Core.IConnectionHandler;
import Core.IConnectionListener;
import java.io.DataInputStream;

/**
 *
 * @author Mr Bao
 */
public class ReaderThread extends Thread {

    private boolean isRunning = true;
    private DataInputStream in;
    private IConnectionListener listener;
    private IConnectionHandler connectionHandler;

    public ReaderThread(DataInputStream in, IConnectionHandler connectionHandler) {
        this.in = in;
        this.connectionHandler = connectionHandler;
    }

    public void SetListener(IConnectionListener listener) {
        this.listener = listener;
    }

    @Override
    public void run() {
        try {
            String newLine;
            while (isRunning && (newLine = in.readLine()) != null) {
                if (listener != null) {
                    listener.IncommingMessage(newLine);
                }
            }
        } catch (Exception e) {
            isRunning = false;
        }
        connectionHandler.Shutdown(false);
    }

    public void Halt() {
        isRunning = false;
    }
}
